#!/usr/bin/env python3
"""
VS Code 调试环境专用测试脚本
"""

import asyncio
import sys
import os

# 确保项目根目录在 Python 路径中
project_root = os.path.dirname(os.path.abspath(__file__))
if project_root not in sys.path:
    sys.path.insert(0, project_root)

print(f"项目根目录: {project_root}")
print(f"Python 路径: {sys.path[:3]}...")

try:
    from app.config import settings
    from app.services.discourse import DiscourseService
    
    print(f"\n配置加载成功:")
    print(f"  Base URL: {settings.DISCOURSE_BASE_URL}")
    print(f"  Username: {settings.DISCOURSE_USERNAME}")
    print(f"  API Key 前10位: {settings.DISCOURSE_API_KEY[:10]}...")
    
except ImportError as e:
    print(f"导入配置失败: {e}")
    import traceback
    traceback.print_exc()
    sys.exit(1)

async def simple_test():
    """简单测试"""
    print("\n=== 开始标签搜索测试 ===")
    
    try:
        service = DiscourseService(
            base_url=settings.DISCOURSE_BASE_URL,
            api_key=settings.DISCOURSE_API_KEY,
            username=settings.DISCOURSE_USERNAME
        )
        
        print("DiscourseService 创建成功")
        
        # 测试搜索
        tags = ['spring']
        print(f"搜索标签: {tags}")
        
        posts = await service.search_posts_by_tags(tags, limit=5)
        print(f"搜索结果: {len(posts)} 个帖子")
        
        if posts:
            for i, post in enumerate(posts[:3], 1):
                print(f"  帖子 {i}:")
                print(f"    ID: {post.get('id')}")
                print(f"    用户: {post.get('username')}")
                print(f"    Topic ID: {post.get('topic_id')}")
                print(f"    创建时间: {post.get('created_at')}")
        else:
            print("  没有找到任何帖子")
            
            # 尝试直接调用客户端
            print("\n尝试直接调用 pydiscourse 客户端:")
            try:
                results = service.client.search("#spring", limit=5)
                direct_posts = results.get('posts', [])
                print(f"  直接搜索找到: {len(direct_posts)} 个帖子")
                
                if direct_posts:
                    print(f"  第一个帖子 ID: {direct_posts[0].get('id')}")
                    
            except Exception as e:
                print(f"  直接搜索也失败: {e}")
        
    except Exception as e:
        print(f"测试失败: {e}")
        import traceback
        traceback.print_exc()

if __name__ == "__main__":
    asyncio.run(simple_test())
